import request from '@/utils/request';
import { download, toFormData, checkDownloadRes } from '@/utils/common';

/**
 * 分页查询配方管理
 */
export async function pageMesFormulaManagements(params) {
  const res = await request.get('/mes/mesFormulaManagement/list', { params });
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 查询全部配方管理
 */
export async function listMesFormulaManagements(params) {
  const res = await request.get('/mes/mesFormulaManagement/list', { params });
  if (res.data.code === 200) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 根据id查询配方管理
 */
export async function getMesFormulaManagement(id) {
  const res = await request.get('/mes/mesFormulaManagement/' + id);
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 添加配方管理
 */
export async function addMesFormulaManagement(data) {
  const res = await request.post('/mes/mesFormulaManagement', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改配方管理
 */
export async function updateMesFormulaManagement(data) {
  const res = await request.put('/mes/mesFormulaManagement', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 删除配方管理
 */
export async function removeMesFormulaManagement(id) {
  const res = await request.delete('/mes/mesFormulaManagement/' + id);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 批量删除配方管理
 */
export async function removeMesFormulaManagements(ids) {
  const res = await request.delete('/mes/mesFormulaManagement/' + ids.join());
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 导出配方管理
 */
export async function exportMesFormulaManagements(params) {
  const res = await request({
    url: '/mes/mesFormulaManagement/export',
    method: 'POST',
    data: toFormData(params),
    responseType: 'blob'
  });
  await checkDownloadRes(res);
  download(res.data, `mesFormulaManagement_${Date.now()}.xlsx`);
}
